
#include "eerom24c32.h"

#include <stdio.h>
#include "esp_log.h"

static const char *TAG = "eerom";

#define WRITE_BIT I2C_MASTER_WRITE              /*!< I2C master write */
#define READ_BIT I2C_MASTER_READ                /*!< I2C master read */
#define ACK_CHECK_EN 0x1                        /*!< I2C master will check ack from slave*/
#define ACK_CHECK_DIS 0x0                       /*!< I2C master will not check ack from slave */
#define ACK_VAL 0x0                             /*!< I2C ack value */
#define NACK_VAL 0x1                            /*!< I2C nack value */



esp_err_t eerom24c32_i2c_master_init(void)
{
    int i2c_master_port = EEROM24C32_I2C_CHANNEL;
    i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = EEROM24C32_MASTER_SDA_IO,
        .sda_pullup_en = GPIO_PULLUP_ENABLE,
        .scl_io_num = EEROM24C32_MASTER_SCL_IO,
        .scl_pullup_en = GPIO_PULLUP_ENABLE,
        .master.clk_speed = EEROM24C32_FREQ_HZ,
        // .clk_flags = 0,          /*!< Optional, you can use I2C_SCLK_SRC_FLAG_* flags to choose i2c source clock here. */
    };
    esp_err_t err = i2c_param_config(i2c_master_port, &conf);
    if (err != ESP_OK) {
        return err;
    }
    return i2c_driver_install(i2c_master_port, conf.mode, 0, 0, 0);
}

/*  
    @brief set time . default time format is 24hours


*/
esp_err_t eerom24c32_read_data(uint16_t addr,uint8_t *data,uint16_t size)
{
    int ret=ESP_OK;
    // uint8_t sec,min,hour,day,date,month,year,data0,data1;
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, EEROM24C32_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, (addr>>8), ACK_CHECK_EN);
    i2c_master_write_byte(cmd, (addr), ACK_CHECK_EN);
    // i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);
    // i2c_master_stop(cmd);
    ret = i2c_master_cmd_begin(EEROM24C32_I2C_CHANNEL, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG,"eeprom write failed!");
        return ret;
    }
    vTaskDelay(30 / portTICK_RATE_MS);

    cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd,EEROM24C32_ADDR << 1 | READ_BIT, ACK_CHECK_EN);
    // vTaskDelay(1/portTICK_PERIOD_MS);
    if(size>1)
    {
        i2c_master_read(cmd,data,size-1,ACK_VAL);
    }
    i2c_master_read_byte(cmd, data+size-1, NACK_VAL);
    i2c_master_stop(cmd);
    ret = i2c_master_cmd_begin(EEROM24C32_I2C_CHANNEL, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);

    // ESP_LOGI(TAG,"read eeprom success!");

    return ret;
}
/* @brief data shoud be a array .lenth should be  7 bytes
    @note  size should be 7
    @note year data range  0-99 , should add 2000 to get correct year 

*/

esp_err_t eerom24c32_write_data(uint16_t addr,uint8_t *data,uint16_t size)
{
        int ret;

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, EEROM24C32_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, (addr>>8), ACK_CHECK_EN);  //send high addr xxxxoooo ooooo
    i2c_master_write_byte(cmd, (addr), ACK_CHECK_EN); //send low addr 32K only need 12bit address
    i2c_master_write(cmd, data,size, ACK_CHECK_EN);
    // i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);
    i2c_master_stop(cmd);
    ret = i2c_master_cmd_begin(EEROM24C32_I2C_CHANNEL, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG,"eerom write data failed!");
        return ret;
    }
    else
    {
        // ESP_LOGI(TAG,"eerom write data success! ");

        return ESP_OK;
    }
    
}